package tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class minDepth {
    public int minDepth(TreeNode root) {
        Deque<TreeNode> que = new LinkedList<>();
        if (root == null){
            return 0;
        }
        que.offerLast(root);
        int deep = 0;
        while (!que.isEmpty()){
            deep++;
            int leveSize = que.size();
            for (int i = 0; i < leveSize; i++) {
                TreeNode poll = que.pollFirst();
                if (poll.left != null){
                    que.offerLast(poll.left);
                }
                if (poll.right != null){
                    que.offerLast(poll.right);
                }
                if (poll.left == null && poll.right == null){
                    return deep;
                }
            }
        }
        return deep;
    }
}
